package lumis.doui.control.validator;

import org.w3c.dom.Node;

import lumis.doui.control.ControlContainer;
import lumis.doui.control.ControlException;
import lumis.doui.control.IControl;
import lumis.doui.control.IDataControl;
import lumis.doui.control.IErrorSummaryControl;
import lumis.doui.control.ValidationMessages;
import lumis.doui.control.ValidatorControl;
import lumis.doui.source.field.type.FileDataType;
import lumis.portal.PortalException;
import lumis.portal.manager.ManagerFactory;
import lumis.util.IResource;
import lumis.util.XmlUtil;

/**
 * File Size Validator
 *
 * @version $Revision: 7882 $ $Date: 2007-08-01 16:45:13 -0300 (Wed, 01 Aug 2007) $
 * @since 4.0.7
 */
public class FileSizeValidatorControl extends ValidatorControl
{
	private Integer minSize = null;
	private Integer maxSize = null;
	
	public void init(Node controlDefinitionNode, ControlContainer controlContainer, IControl parentControl) throws PortalException
	{
		super.init(controlDefinitionNode, controlContainer, parentControl);

		String minSize = XmlUtil.readAttributeString("minSize", controlDefinitionNode);
		String maxSize = XmlUtil.readAttributeString("maxSize", controlDefinitionNode);

		if (minSize != null)
			this.minSize = new Integer(minSize);

		if (maxSize != null)
			this.maxSize = new Integer(maxSize);
	}
	
	public boolean isClientSideValidationEnabled()
	{
		return false;
	}

	public String getScriptValidationFunctionImplementation() throws PortalException
	{
		// TODO Auto-generated method stub
		return null;
	}

	public boolean doServerValidation() throws PortalException
	{
		IControl control = getControlToValidate();
		
		if(!(control instanceof IDataControl))
			throw new ControlException("STR_VALIDATING_NON_DATA_CONTROL");
		
		IDataControl controlToValidate = (IDataControl)control;
		
		FileDataType.Data controlToValidateValue = controlToValidate.getConverter().convert(FileDataType.Data.class, controlToValidate.getValue());
		if (controlToValidateValue != null)
		{
			long valueSize = controlToValidateValue.getFileSize();
			if (minSize != null && valueSize < minSize.intValue())
			{
				messages = getMessages("belowMinimumSize");
				controlToValidate.setValid(false);
				IErrorSummaryControl errorSummaryControl = controlContainer.getErrorSummaryControl();
				errorSummaryControl.addErrorMessage(ManagerFactory.getLocalizationManager().localize(messages.getErrorMessage(), getControlResource()));
				return false;
			}
			else if (maxSize != null && valueSize > maxSize.intValue())
			{
				messages = getMessages("aboveMaximumSize");
				controlToValidate.setValid(false);
				IErrorSummaryControl errorSummaryControl = controlContainer.getErrorSummaryControl();
				errorSummaryControl.addErrorMessage(ManagerFactory.getLocalizationManager().localize(messages.getErrorMessage(), getControlResource()));
				return false;
			}
		}
		return true;
	}

	protected ValidationMessages getDefaultMessages(String errorType) throws PortalException
	{
		if ("belowMinimumSize".equals(errorType))
			return new ValidationMessages(ValidatorControl.DEFAULT_VALUE, "STR_FILE_SIZE_BELOW_MINIMUM", getDefaultMessageParameters());
		if ("aboveMaximumSize".equals(errorType))
			return new ValidationMessages(ValidatorControl.DEFAULT_VALUE, "STR_FILE_SIZE_ABOVE_MAXIMUM", getDefaultMessageParameters());
		return new ValidationMessages(ValidatorControl.DEFAULT_VALUE, "STR_FILE_SIZE_MUST_BE_BETWEEN", getDefaultMessageParameters());
	}

	protected String[] getDefaultMessageParameters() throws PortalException
	{
		String[] parameters = null;
		
		String minSizeParam = "";
		if(minSize != null)
			minSizeParam = minSize.toString();
		
		String maxSizeParam = "";
		if(maxSize != null)
			maxSizeParam = maxSize.toString();
		
		if(minSize != null && maxSize != null)
			parameters = new String[]{ getControlToValidateSourceFieldName(), minSizeParam, maxSizeParam};
		else if(minSize != null)
			parameters = new String[]{ getControlToValidateSourceFieldName(), minSizeParam};
		else
			parameters = new String[]{ getControlToValidateSourceFieldName(), maxSizeParam};
		
		return parameters;
	}
	
	public IResource getControlResource() throws PortalException
	{
		if(controlResource == null)
		{
			controlResource = super.getControlResource();
			controlResource.addResourcePath("lumis/doui/control/validator/strings/filesizestrings");
		}
		
		return controlResource;
	}
}
